home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
WINSOUND
/
CHIMES.ZIP
/
MAIN.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-10
|
18KB
|
1,046 lines
#include "main.h"
static char *Profile = CHIMEINI;
static char *Version = CHIMEVER;
CMyMainDialog::CMyMainDialog(CWnd *parentWnd)
{
RECT dr;
RECT pcr;
RECT pwr;
p_gotProfile = FALSE;
Create("main", parentWnd);
GetClientRect(&dr);
parentWnd->GetWindowRect(&pwr);
parentWnd->GetClientRect(&pcr);
pwr.right += dr.right - (pcr.right - pcr.left);
pwr.bottom += dr.bottom - (pcr.bottom - pcr.top);
parentWnd->MoveWindow(&pwr);
}
void
CMyMainDialog::cleanup()
{
// close MIDI
if (p_hMidi) {
midiOutClose(p_hMidi);
}
if (p_gotProfile) {
saveDefaults();
}
}
void
CMyMainDialog::saveDefaults()
{
char *buf = new char[30];
char *buf2 = new char[30];
int i;
wsprintf(buf, "%d", p_channel + 1);
WritePrivateProfileString(Version, "channel",
buf, Profile);
for (i = 0; i < 16; i++) {
wsprintf(buf, "program%d", i + 1);
wsprintf(buf2, "%d", p_program[i]);
WritePrivateProfileString(Version,
buf, buf2, Profile);
}
wsprintf(buf, "%d", p_minVol);
WritePrivateProfileString(Version, "minvol", buf, Profile);
wsprintf(buf, "%d", p_maxVol);
WritePrivateProfileString(Version, "maxvol", buf, Profile);
wsprintf(buf, "%d", p_minLength);
WritePrivateProfileString(Version, "minlength", buf, Profile);
wsprintf(buf, "%d", p_maxLength);
WritePrivateProfileString(Version, "maxlength", buf, Profile);
wsprintf(buf, "%d", p_lowNote);
WritePrivateProfileString(Version, "lownote", buf, Profile);
wsprintf(buf, "%d", p_highNote);
WritePrivateProfileString(Version, "highnote", buf, Profile);
wsprintf(buf, "%d", p_restProb);
WritePrivateProfileString(Version, "rest", buf, Profile);
for (i = 0; i < 128; i++) {
wsprintf(buf, "note%d", i + 1);
wsprintf(buf2, "%d", p_noteProb[i]);
WritePrivateProfileString(Version, buf, buf2, Profile);
}
delete buf;
delete buf2;
}
BOOL
CMyMainDialog::OnInitDialog()
{
UINT merr;
int i;
char *buf = new char[30];
CDialog::OnInitDialog();
p_playing = FALSE;
p_note = -1;
for (i = 0; i < 16; i++) {
wsprintf(buf, "program%d", i + 1);
p_program[i] = GetPrivateProfileInt(Version, buf, -1,
Profile);
}
p_channel = GetPrivateProfileInt(Version, "channel", 1, Profile) - 1;
p_lastChannel = 255;
channelSB()->SetScrollRange(0, 15);
channelChange(-1);
programSB()->SetScrollRange(-1, 127);
programChange(p_program[p_channel]);
p_minVol = GetPrivateProfileInt(Version, "minvol", 90, Profile);
p_maxVol = GetPrivateProfileInt(Version, "maxvol", 120, Profile);
minvolSB()->SetScrollRange(0, 127);
minVolChange(p_minVol, FALSE);
maxvolSB()->SetScrollRange(0, 127);
maxVolChange(p_maxVol, FALSE);
p_minLength = GetPrivateProfileInt(Version, "minlength", 100, Profile);
p_maxLength = GetPrivateProfileInt(Version, "maxlength", 500, Profile);
minlenSB()->SetScrollRange(1, 3000);
minlenSB()->SetScrollPos(p_minLength);
minLengthChange(p_minLength, FALSE);
maxlenSB()->SetScrollRange(1, 3000);
maxlenSB()->SetScrollPos(p_maxLength);
maxLengthChange(p_maxLength, FALSE);
p_lowNote = GetPrivateProfileInt(Version, "lownote", DMN_C_3, Profile);
p_highNote = GetPrivateProfileInt(Version, "highnote", DMN_C_6, Profile);
lownoteSB()->SetScrollRange(DMN_C_0, DMN_G_10);
lownoteSB()->SetScrollPos(p_lowNote);
lowNoteChange(p_lowNote, FALSE);
highnoteSB()->SetScrollRange(DMN_C_0, DMN_G_10);
highnoteSB()->SetScrollPos(p_highNote);
highNoteChange(p_highNote, FALSE);
p_restProb = GetPrivateProfileInt(Version, "rest", 10, Profile);
restSB()->SetScrollRange(0, 100);
restChange(p_restProb);
merr = midiOutOpen(&p_hMidi, MIDIMAPPER, (DWORD)(UINT)m_hWnd, 0, CALLBACK_WINDOW);
if (merr) {
char mmesg[80];
midiOutGetErrorText(merr, mmesg, sizeof(mmesg));
MessageBox(mmesg, "MIDI Mapper",
MB_OK | MB_ICONSTOP);
PostQuitMessage(0);
}
srand((unsigned)time(NULL));
for (i = 0; i< 128 ; i++) {
wsprintf(buf, "note%d", i + 1);
p_noteProb[i] = GetPrivateProfileInt(Version, buf, 50, Profile);
}
p_gotProfile = TRUE;
delete buf;
return TRUE;
}
void
CMyMainDialog::OnPlayStop()
{
playstop();
}
void
CMyMainDialog::playstop()
{
if (p_playing) {
playPB()->SetWindowText("Play");
p_playing = FALSE;
} else {
playPB()->SetWindowText("Stop");
p_playing = TRUE;
program();
nextNote();
}
}
//
// program
//
void
CMyMainDialog::program()
{
if (p_program[p_channel] >= 0 && p_program[p_channel] <= 127) {
dmidiSendChannelMessage(p_hMidi, p_channel,
DMM_PROGRAM, (BYTE)(p_program[p_channel]), 0);
}
}
BYTE
CMyMainDialog::getVolume()
{
int range;
range = (p_maxVol - p_minVol) + 1;
return (p_minVol + (rand() % range));
}
UINT
CMyMainDialog::getLength()
{
int range;
range = (p_maxLength - p_minLength) + 1;
return (p_minLength + (rand() % range));
}
int
CMyMainDialog::getNote()
{
int range;
int rnum;
int i;
long sum;
rnum = rand();
if ((WORD)(rnum % 100) < p_restProb) {
return -1; // rest
}
range = p_highNote - p_lowNote;
// sum the probabilities
sum = 0;
for (i = p_lowNote; i < (int)p_highNote; i++) {
sum += p_noteProb[i];
}
if (sum == 0) {
return -1; // rest
}
// Now, find the slot that has that probability
rnum %= sum;
i = p_lowNote;
while (rnum > 0) {
rnum -= p_noteProb[i];
i++;
}
return i;
}
//
// nextNote
//
void
CMyMainDialog::nextNote()
{
BYTE volume;
UINT length;
p_note = getNote();
volume = getVolume();
length = getLength();
if (p_note >= 0) {
dmidiSendChannelMessage(p_hMidi, p_channel,
DMM_NOTE_ON, p_note, volume);
}
SetTimer(99, length, NULL);
}
void
CMyMainDialog::OnTimer(UINT)
{
BYTE sChannel;
if (p_note >= 0) {
if (p_lastChannel != 255) {
sChannel = p_lastChannel;
p_lastChannel = 255;
} else {
sChannel = p_channel;
}
dmidiSendChannelMessage(p_hMidi, sChannel,
DMM_NOTE_OFF, p_note, 0);
}
if (p_playing) {
nextNote();
}
}
BOOL
CMyMainDialog::shifted()
{
int ks;
ks = GetKeyState(VK_SHIFT);
if ((ks & 0x8000) != 0) {
return TRUE;
}
return FALSE;
}
void
CMyMainDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pSb)
{
int minv, maxv, value;
BOOL gang = shifted();
pSb->GetScrollRange(&minv, &maxv);
value = pSb->GetScrollPos();
switch (nSBCode) {
case SB_LINELEFT:
if (value > minv) {
value--;
}
break;
case SB_LINERIGHT:
if (value < maxv) {
value++;
}
break;
case SB_LEFT:
value = minv;
break;
case SB_RIGHT:
value = maxv;
break;
case SB_PAGELEFT:
value -= (maxv - minv) / 10;
if (value < minv) {
value = minv;
}
break;
case SB_PAGERIGHT:
value += (maxv - minv) / 10;
if (value > maxv) {
value = maxv;
}
break;
case SB_THUMBPOSITION:
value = nPos;
break;
default:
return;
}
// handle the operation
if (pSb == channelSB()) {
channelChange(value);
return;
}
if (pSb == programSB()) {
programChange(value);
return;
}
if (pSb == minvolSB()) {
minVolChange(value, gang);
return;
}
if (pSb == maxvolSB()) {
maxVolChange(value, gang);
return;
}
if (pSb == minlenSB()) {
minLengthChange(value, gang);
return;
}
if (pSb == maxlenSB()) {
maxLengthChange(value, gang);
return;
}
if (pSb == lownoteSB()) {
lowNoteChange(value, gang);
return;
}
if (pSb == highnoteSB()) {
highNoteChange(value, gang);
return;
}
if (pSb == restSB()) {
restChange(value);
return;
}
}
void
CMyMainDialog::channelChange(int val)
{
char *buf = new char[30];
if (val >= 0) {
if (p_lastChannel == 255) {
p_lastChannel = p_channel;
}
p_channel = val;
}
wsprintf(buf, "%d", p_channel + 1);
channelST()->SetWindowText(buf);
channelSB()->SetScrollPos(p_channel);
programChange(p_program[p_channel]);
delete buf;
}
void
CMyMainDialog::programChange(i